import json, sys, MySQLdb
from tweetParser import *

fileName = "NYC_TEST_201104050942"#"".join(sys.argv[1])
f = open(fileName)
p = parser()

try:
	conn = MySQLdb.connect (host = "tweetographer.db.7281171.hostedresource.com",
				user = "tweetographer",
				passwd = "CS2012Tweeter",
				db = "tweetographer")
except MySQLdb.Error, e:
	print "Error %d: %s" % (e.args[0], e.args[1])
	sys.exit (1)

cursor = conn.cursor()

cursor.execute("SELECT Max(TweetID) FROM Tweets")
accumulator = cursor.fetchone()[0] + 1

print accumulator
raw_input("Press Enter to continue...")

for line in f:
	try:
		content = json.loads(line)
		longitude = None
		latitude = None
		if "text" in content and p.isGood("{0[text]}".format(content)):
			username = "{0[user][screen_name]}".format(content)
			text = "{0[text]}".format(content)			
			try:
				coordinates = "{0[coordinates][coordinates]}".format(content).split(',')
				longitude = float(coordinates[0].replace('[',''))
				latitude = float(coordinates[1].replace(']',''))
				print u"{0[user][screen_name]}: {0[text]} {0[coordinates][coordinates]}".format(content)
			except TypeError:
				pass

			#if longitude is not None and latitude is not None:
			#	cursor.execute ("""
			#			INSERT INTO Tweets (TweetID, Username, Text, Longitude, Latitude)
			#			VALUES
			#			(%s,%s,%s,%s,%s)
			#			""",(accumulator,username,text,longitude,latitude))
			#	accumulator += 1
			#	#print username,text,longitude,latitude
			#else:
			#	cursor.execute("""
			#			INSERT INTO Tweets (TweetID,Username,Text)
			#			VALUES
			#			(%s,%s,%s)
			#			""",(accumulator,username,text))
			#	accumulator += 1
				#print username, text
	except ValueError:
		pass

conn.close()
print "Complete."

